/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Unless required by applicable law or
 * agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package org.rhok.pdx.ncn.listeners;

import java.util.List;
import java.util.Queue;

import org.rhok.pdx.ncn.R;
import org.rhok.pdx.ncn.datatypes.LocationData;
import org.rhok.pdx.ncn.datatypes.MapOverlay;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * Utility to drop dots on the map relating to signal intensity.
 * 
 * @author afitzgibbon
 */
public class MapPlotter extends Thread {
    
    /**
     * Delay, in milliseconds, between successive attempts to draw an intensity dot.
     */
    private static final int DROP_DOT_DELAY = 5000;
    
    /**
     * The Interface MapPlotterContext.
     */
    public interface MapPlotterContext {
        public Queue<LocationData> locs();
        public Activity app();
        public MapView mapView();
    }
    
    private MapPlotterContext context;
    private MapOverlay overlay = null;
    private boolean running = true;
    private Drawable drawable;
    private GeoPoint point;
    private OverlayItem item;
    private List<Overlay> mapOverlays;
    private MapView mapView;
    
    /**
     * Instantiates a new map plotter.
     *
     * @param context the context
     */
    public MapPlotter(MapPlotterContext context) {
        this.context = context;
    }
    
    /**
     * Run the actual plotting thread. Grabs the most recent local point and adds a dot in it's
     * location. After dot is added, wait of few seconds before trying the next one.
     */
    public void run() {
        drawable = context.app().getResources().getDrawable(R.drawable.greendot);
        overlay = new MapOverlay(drawable);
        mapView = context.mapView();
        mapOverlays = mapView.getOverlays();
        
        while (running) {
            // pop queue
            LocationData locDat = context.locs().poll();
            if (locDat != null) {
                Location loc = locDat.loc();

                // add point
                point = new GeoPoint((int)(loc.getLatitude()*1E6), (int)(loc.getLongitude()*1E6));
                item = new OverlayItem(point, "", "");
                
                context.app().runOnUiThread(new Runnable() {
                    public void run() {
                        overlay.addOverlay(item);
                        mapOverlays.add(overlay);
                        mapView.invalidate();
                    }
                });
            }
            
            try {
                sleep(DROP_DOT_DELAY);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
